package com.monodev.mvc.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

@Controller
public class HelloworldController {

    @RequestMapping("/check")
    public ModelAndView count(HttpServletRequest request){  //DEMO，关于session的使用
        HttpSession session = request.getSession();
        Object object = session.getAttribute("count");

        if (object == null){
            session.setAttribute("count", 0);
        } else {
            int count = (int) session.getAttribute("count");
            count ++;
            session.setAttribute("count", count);
        }

        int count = (int) session.getAttribute("count");
        ModelAndView view = new ModelAndView("count");
        view.addObject("countNum", count);
        return view;
    }

    @RequestMapping("/clear")
    public ModelAndView clear(HttpServletRequest request){
        HttpSession session = request.getSession();
        session.setAttribute("count", null);

        ModelAndView view = new ModelAndView("redirect:/check");
        return view;
    }

}
